package com.jsm.tf.net.protocol.serialize;

import lombok.Cleanup;

import java.io.*;

public class JDKSerializer implements Serializer {
    @Override
    public <T> T deserialize(Class<T> clazz, byte[] bytes) throws IOException {
        @Cleanup ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        @Cleanup ObjectInputStream ois = new ObjectInputStream(bis);
        try {
            return (T) ois.readObject();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public <T> byte[] serialize(T object) throws IOException {
        @Cleanup  ByteArrayOutputStream bos = new ByteArrayOutputStream();
        @Cleanup   ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(object);
        return bos.toByteArray();
    }
}